package com.java46.admin.base.util;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.query.NativeQuery;
import org.hibernate.query.Query;

public class HibernateUtils {
    private static SessionFactory sessionFactory=null;
    static {
        Configuration configure = new Configuration().configure();
        sessionFactory = configure.buildSessionFactory();
    }
    /**
     * 用于获取session
     */

    public static Session getSession(){
        Session session = sessionFactory.openSession();
        return  session;
    }

    /**
     * 用于获取query
     * @param hql
     * @return
     */
    public static Query getQuery(String hql){
        Session session= getSession();
        Query query = session.createQuery(hql);
        return query;
    }


    //用于获取Native
    public static NativeQuery getNativeQuery(String hql){
        Session session= getSession();
        NativeQuery nativeQuery = session.createNativeQuery(hql);
        return nativeQuery;
    }
}
